package com.bwie.service.impl;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.bwie.mapper.OrderDetailMapper;
import com.bwie.mapper.ProdMapper;
import com.bwie.poi.ExcelUtil;
import com.bwie.pojo.Order;
import com.bwie.pojo.OrderDetail;
import com.bwie.pojo.Prod;
import com.bwie.service.OrderService;
import com.bwie.mapper.OrderMapper;
import com.bwie.utils.Result;
import com.bwie.vo.OrderVo;
import io.jsonwebtoken.Claims;
import io.jsonwebtoken.Jwts;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Date;
import java.util.List;

/**
* @author 陈彤彤
* @description 针对表【t_order(订单表)】的数据库操作Service实现
* @createDate 2025-07-19 09:15:14
*/
@Service
public class OrderServiceImpl extends ServiceImpl<OrderMapper, Order>
    implements OrderService{

    @Autowired
    OrderMapper orderMapper;

    @Autowired
    OrderDetailMapper orderDetailMapper;

    @Autowired
    HttpServletRequest request;

    @Autowired
    HttpServletResponse response;

    @Autowired
    ProdMapper prodMapper;

    @Autowired
    StringRedisTemplate redisTemplate;

    /**
    * @param orderVo:
    * @return Result
    * @author :陈彤彤
    * @description 分页查询订单信息
    * @date 2025/7/23 16:50
    */
    @Override
    public Result orderList(OrderVo orderVo) {
        Page<Order> orderPage = new Page<>(orderVo.getPageNum(), orderVo.getPageSize());

        Page<Order> page = orderMapper.orderList(orderPage, orderVo);

        /*导出数据存入redis*/
        String token = request.getHeader("token");
        Claims claims = Jwts.parser().setSigningKey("123456").parseClaimsJws(token).getBody();
        Integer userId = claims.get("userId", Integer.class);
        String key = "order-" + userId;
        redisTemplate.opsForValue().set(key, JSON.toJSONString(orderPage.getRecords()));

        return Result.ok().setData(page);
    }

    /**
    * @param ids:
    * @return Result
    * @author :陈彤彤
    * @description 批量删除订单信息
    * @date 2025/7/23 19:34
    */
    @Override
    public Result deleteOrderBath(List<Integer> ids) {
        /*删除订单表*/
        orderMapper.deleteBatchIds(ids);

        /*删除商品Id*/
        prodMapper.deleteBatchIds(ids);

        /*删除订单详情表*/
        orderDetailMapper.delete(new QueryWrapper<OrderDetail>().lambda().in(OrderDetail::getOrderId,ids));
        return Result.ok();
    }

    /**
    * @param userId:
    * @return void
    * @author :陈彤彤
    * @description 导出订单信息
    * @date 2025/7/23 20:50
    */
    @Override
    public void exportOrder(Integer userId) {
        /*获取Key*/
        String key = "order-" + userId;
        /*从redis中获取*/
        String s = redisTemplate.opsForValue().get(key);

        /*转换JSON格式*/
        List<Order> orders = JSON.parseArray(s, Order.class);
        /*导出*/
        ExcelUtil<Order> excelUtil = new ExcelUtil<>(Order.class);
        excelUtil.exportExcel(response,orders,"订单列表");
    }

    /**
    * @param order:
    * @return Result
    * @author :陈彤彤
    * @description 添加订单信息
    * @date 2025/7/23 21:06
    */
    @Override
    public Result addOrder(Order order) {
        order.setCreateBy("system");
        order.setCreateTime(new Date());
        order.setUpdateBy("system");
        order.setUpdateTime(new Date());
        orderMapper.insert(order);
        return Result.ok();
    }

    /**
    * @param :
    * @return Result
    * @author :陈彤彤
    * @description 查询所有商品信息
    * @date 2025/7/23 21:35
    */
    @Override
    public Result selectProd() {
        List<Prod> prodList = prodMapper.selectList(null);
        return Result.ok().setData(prodList);
    }
}




